﻿using Sidvall.Logging;
using Sidvall.LogManager.Net.Services.V1;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sidvall.LogManager
{
    public class Log : Sidvall.Logging.LogBase
    {
        #region Public Members

        public long LogScopeId { get; set; }

        #region DeleteItemsAsync

        public async Task<int> DeleteItemsAsync(Sidvall.Logging.EventType? eventType,
            string category, string textFilter, System.DateTime? expiry)
        {
            return await Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.DeleteLogScopeItemsAsync(null, this.LogScopeId, eventType,
                category, textFilter, expiry).ConfigureAwait(false);
        }

        #endregion
        #region GetItemsAsync

        public async Task<LogScopeItemCollectionItem> GetItemsAsync(Sidvall.Logging.EventType? eventType,
            string category, string textFilter, System.DateTime? expiry,
            int? rowCount, int? rowStartIndex, IEnumerable<Sidvall.Data.SortItem> sortItems)
        {
            return await Sidvall.LogManager.SystemContext.Current.DataPortal.LogScopeItemManager.GetItemsAsync(null, this.LogScopeId, eventType,
                category, textFilter, expiry,
                rowCount, rowStartIndex, sortItems).ConfigureAwait(false);
        }

        #endregion
        #region WriteAsync

        public override async Task WriteAsync(string title, string message, IEnumerable<string> categories, EventType severity, IDictionary<string, string> properties)
        {
            if (!this.EnabledCategories.Contains(categories, severity))
                return;
            await WriteAsync(this.LogScopeId, title, message, categories, severity, properties).ConfigureAwait(false);
        }
        public static async Task WriteAsync(long logScopeId, string title, string message, IEnumerable<string> categories, EventType severity, IDictionary<string, string> properties)
        {
            title = Sidvall.Business.ValidationManager.GetValidString(title, false, null);
            message = Sidvall.Business.ValidationManager.GetValidString(message, false, null);
            var logItem = new LogScopeItem()
            {
                CreatedTime = System.DateTime.UtcNow,
                EventTypeId = severity,
                LogScopeItemCategories = new LogScopeItemCategoryCollectionItem(),
                LogScopeId = logScopeId,
                Message = message ?? string.Empty,
                Title = title ?? string.Empty,
                IdentityAuthenticationType = Sidvall.SystemContext.Current.PrincipalManager.CurrentIdentityAuthenticationType ?? string.Empty,
                IdentityName = Sidvall.SystemContext.Current.PrincipalManager.CurrentIdentityName ?? string.Empty,
            };
            if (categories != null)
            {
                foreach (var category in categories)
                {
                    var logItemCategory = new LogScopeItemCategory()
                    {
                        Category = category,
                    };
                    logItem.LogScopeItemCategories.Add(logItemCategory);
                }
            }
            logItem = await logItem.SaveItemAsync().ConfigureAwait(false);
        }

        #endregion

        #endregion
        #region Constructors

        public Log()
        {
        }
        public Log(bool includeWarning, bool includeInformation) : base(includeWarning, includeInformation)
        {
        }
        public Log(IEnumerable<string> categories, IEnumerable<EventType> eventTypes) : base(categories, eventTypes)
        {
        }
        public Log(long logScopeId)
        {
            this.LogScopeId = logScopeId;
        }
        public Log(long logScopeId, bool includeWarning, bool includeInformation) : base(includeWarning, includeInformation)
        {
            this.LogScopeId = logScopeId;
        }
        public Log(long logScopeId, IEnumerable<string> categories, IEnumerable<EventType> eventTypes) : base(categories, eventTypes)
        {
            this.LogScopeId = logScopeId;
        }

        #endregion
    }
}
